package com.cx.mall.user.service.impl;

import com.cx.mall.common.util.ResponseEnum;
import com.cx.mall.user.entity.UserInfo;
import com.cx.mall.user.entity.UserLoginLog;
import com.cx.mall.user.mapper.IUserLoginLogMapper;
import com.cx.mall.user.service.ILoginService;
import com.cx.mall.user.service.IUserInfoService;
import com.cx.mall.user.service.IUserLoginLogService;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @version 1.0
 * @Description TODO
 * @Author ke
 * @Since 2025/6/11 8:17
 */
@Service
public class LoginServiceImpl implements ILoginService {
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IUserLoginLogService  userLoginLogService;
    @Override
    public UserInfo login(String username, String password) {
        // 到user_info表中查询username
        UserInfo userInfo = userInfoService.queryByUsername(username);
        if(userInfo == null){
            throw  new RuntimeException(ResponseEnum.USER_ACCOUNT_FORBIDDEN.getMessage());
        }
        // 比较密码
        if(!BCrypt.checkpw(password,userInfo.getPassword())){
            throw new RuntimeException(ResponseEnum.USER_PASSWORD_ERROR.getMessage());
        }
        // 写日志
        userLoginLogService.writeLoginLog(userInfo.getId());
        return userInfo;
    }
}
